package com.yunnuo.system.dict.service.impl;

import com.yunnuo.system.comm.util.Empty;
import com.yunnuo.system.comm.util.Where;
import com.yunnuo.system.comm.util.page.TableDataInfo;
import com.yunnuo.system.dict.dao.IDictDataDao;
import com.yunnuo.system.dict.dao.IDictTypeDao;
import com.yunnuo.system.dict.entity.DictType;
import com.yunnuo.system.dict.service.IDictTypeService;
import com.yunnuo.system.users.entity.PageParams;
import org.hibernate.service.spi.ServiceException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by w_weihf on 2018/8/3.
 */
@Service
public class DictTypeServiceImpl implements IDictTypeService {

    @Autowired
    private IDictTypeDao dictTypeDao;

    @Override
    public void addVO(DictType vo) throws ServiceException {
        dictTypeDao.save(vo);
    }

    @Override
    public void updateVO(DictType vo) throws ServiceException {
        dictTypeDao.saveAndFlush(vo);
    }

    @Override
    public void delList(String[] ids) throws ServiceException {
    }

    @Override
    public void deleteVo(String id) throws ServiceException {
        dictTypeDao.delete(id);
    }

    @Override
    public List<DictType> findList() throws ServiceException {
        return dictTypeDao.findAll();
    }

    @Override
    public List<?> findList(DictType vo, String other) throws ServiceException {
        return null;
    }

    @Override
    public DictType findVO(String id) throws ServiceException {
        return dictTypeDao.findOne(id);
    }

    @Override
    public TableDataInfo selectDictTypeByPageParams(PageParams pageParams, PageRequest pageRequest) {
        TableDataInfo dataInfo=new TableDataInfo();
        Specification<DictType> specification=new Specification<DictType>() {
            @Override
            public Predicate toPredicate(Root<DictType> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
                Path<String> namePath=root.get("dictName");
                String value=pageParams.getSearchValue();
                if (Empty.isNotEmpty(value)){
                    query.where(cb.like(namePath,"%"+value+"%"));
                }
                return null;
            }
        };
        Page<DictType> dictTypes=dictTypeDao.findAll(specification,pageRequest);
        dataInfo.setRows(dictTypes.getContent());
        dataInfo.setTotal(dictTypes.getTotalElements());
        return dataInfo;
    }
}
